<template>

  <div id="robot">
    <el-row>
      <mu-appbar title="机器人配置" style="background-color:#409EFF">
        <mu-icon-button  icon="arrow_back_ios" slot="left" :to="{name: 'GmMenu' }"/>
      </mu-appbar>
    </el-row>

    <el-tabs type="border-card">
      <el-tab-pane label="概率配置">
        <div class= "tools" v-show="tools">
          <el-row>
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <el-input style="width:200px;" v-model="condition.uid" @blur="handleFind" placeholder="输入账号"></el-input>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <el-input style="width:200px;" v-model="condition.inDeskId" @blur="handleFind" placeholder="输入桌子Id"></el-input>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <el-select style="width:200px;" v-model="condition.type" placeholder="请选择" @change="handleFind">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <el-input style="width:200px;" v-model="globalDymcPro" placeholder="全局动态概率"></el-input>
              <el-button type="primary" @click="updateGlobalDymcPro">修改</el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <span>类型2机器人总输赢：{{robotTotalWinLoseNdot}}</span>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <span>使用中的类型1机器人：{{total1}}</span>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6">
              <span>使用中的类型2机器人：{{total2}}</span>
            </el-col>
          </el-row>
        </div>

        <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="accountId"
            label="账号ID">
          </el-table-column>
          <el-table-column
            prop="level"
            label="等级">
          </el-table-column>
          <el-table-column
            prop="gameId"
            label="游戏名称">
          </el-table-column>
          <el-table-column
            prop="trait"
            label="性格">
          </el-table-column>
          <el-table-column
            prop="property"
            label="属性位标志">
          </el-table-column>
          <el-table-column
            prop="used"
            label="是否使用中">
            <template slot-scope="scope">
              <span v-if="scope.row.used === false" style="color:red">未使用</span>
              <span v-if="scope.row.used === true" style="color:green">在使用</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="inDeskId"
            label="桌子">
          </el-table-column>
          <el-table-column
            prop="basePro"
            label="基础概率">
          </el-table-column>
          <el-table-column
            prop="dymcPro"
            label="动态概率">
          </el-table-column>
          <el-table-column
            prop="wlNdot"
            label="净输赢">
            <template slot-scope="scope">
              <span v-if="scope.row.type === 1" >无</span>
              <span v-if="scope.row.type === 2" >{{scope.row.wlNdot}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type === 1" type="primary" @click="showUpdateDymcProDialog(scope.row.accountId, scope.row.dymcPro)" disabled>修改动态概率</el-button>
              <el-button v-if="scope.row.type === 2" type="primary" @click="showUpdateDymcProDialog(scope.row.accountId, scope.row.dymcPro)">修改动态概率</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-row>
          <center>
            <el-pagination
              small
              layout="prev, pager, next"
              :total="condition.total"
              :page-size="condition.pageSize"
              :current-page="condition.currentPage"
              @current-change="handleCurrentChange">
            </el-pagination>
          </center>
        </el-row>

        <el-dialog title="修改动态概率" :visible.sync="dialogUpdateDymcPro">

          <el-row>
            <el-col :span="6">
              <span>账号</span>
            </el-col>
            <el-col :span="18">
              <el-input v-model="updateDymcPro.id" size="mini" disabled ></el-input>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <span>动态概率</span>
            </el-col>
            <el-col :span="18">
              <el-input v-model="updateDymcPro.pro" size="mini" ></el-input>
            </el-col>
          </el-row>

          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogUpdateDymcPro = false">取 消</el-button>
            <el-button type="primary" @click="handleUpdateDymcPro">确 定</el-button>
          </div>
        </el-dialog>
      </el-tab-pane>

      <el-tab-pane label="性格配置">
        <el-table
          :data="tableRrait"
          style="width: 100%">
          <el-table-column
            prop="config.pros.p1"
            label="上局赢退出概率">
          </el-table-column>
          <el-table-column
            prop="config.pros.p2"
            label="上局输退出概率">
          </el-table-column>
          <el-table-column
            prop="config.pros.p3"
            label="上局流局退出概率">
          </el-table-column>
          <el-table-column
            prop="config.pros.p4"
            label="超过3秒未开始退出概率（大米）">
          </el-table-column>
          <el-table-column
            prop="config.pros.p5"
            label="超过5秒未开始退出概率（大米）">
          </el-table-column>
          <el-table-column
            prop="config.pros.p6"
            label="超过8秒未开始退出概率（大米）">
          </el-table-column>
          <el-table-column
            prop="config.pros.p7"
            label="游戏开始等待3秒未结束退出概率（小米与8点9点）">
          </el-table-column>
          <el-table-column
            prop="config.pros.p8"
            label="游戏开始等待8秒未结束退出概率（小米与8点9点）">
          </el-table-column>
          <el-table-column
            prop="config.pros.p9"
            label="游戏开始等待20秒未结束退出概率（小米与8点11点）">
          </el-table-column>
          <el-table-column
            prop="config.pros.p10"
            label="玩家扔标签AI回应概率">
          </el-table-column>
          <el-table-column
            prop="config.pros.p11"
            label="玩家出票慢扔表情概率">
          </el-table-column>
          <el-table-column
            prop="config.pros.p12"
            label="AI胜率发表情概率">
          </el-table-column>
          <el-table-column
            prop="config.pros.p13"
            label="玩家第一名胜率发表情概率（小米）">
          </el-table-column>
          <el-table-column
            prop="config.pros.p14"
            label="AI下家跟牌，对下家扔表情概率">
          </el-table-column>
          <el-table-column
            prop="config.pros.p15"
            label="他人连续三回合交互概率（大米）">
          </el-table-column>
          <el-table-column
            prop="trait"
            label="性格类型">
          </el-table-column>
          <el-table-column
            prop="traitDesc"
            label="性格描述">
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button type="primary" @click="showUpdateTraitDialog(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>


      <el-dialog title="修改性格" :visible.sync="dialogUpdateTrait">

        <el-row>
          <el-col :span="6">
            <span>上局赢退出概率</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p1" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>上局输退出概率</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p2" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>上局流局退出概率</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p3" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>超过3秒未开始退出概率（大米）</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p4" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>超过5秒未开始退出概率（大米）</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p5" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>超过8秒未开始退出概率（大米）</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p6" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>游戏开始等待3秒未结束退出概率（小米与8点9点）</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p7" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>游戏开始等待8秒未结束退出概率（小米与8点9点）</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p8" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>游戏开始等待20秒未结束退出概率（小米与8点11点）</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p9" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>玩家扔标签AI回应概率</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p10" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>玩家出票慢扔表情概率</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p11" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>AI胜率发表情概率</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p12" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>玩家第一名胜率发表情概率（小米）</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p13" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>AI下家跟牌，对下家扔表情概率</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p14" size="mini"></el-input>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <span>他人连续三回合交互概率（大米）</span>
          </el-col>
          <el-col :span="18">
            <el-input v-model="traitConfig.config.pros.p15" size="mini"></el-input>
          </el-col>
        </el-row>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogUpdateTrait = false">取 消</el-button>
          <el-button type="primary" @click="handleUpdateTrait">确 定</el-button>
        </div>
      </el-dialog>

    </el-tabs>

  </div>

</template>

<style>
#robot .el-row {
  margin-bottom: 16px;
}
#robot .el-col {
  margin-bottom: 8px;
  margin-top: 8px;
}
</style>

<script>
  import moment from 'moment'
  import Qs from 'qs'
  export default {
    data () {
      return {
        tools: true,
        tableData: [],
        condition: {
          uid: '',
          inDeskId: '',
          type: 2,
          currentPage: 1,
          pageSize: 10,
          total: 0
        },
        dialogUpdateDymcPro: false,
        globalDymcPro: '',
        updateDymcPro: {
          id: '',
          pro: ''
        },
        options: [{
          value: 1,
          label: '类型1'
        }, {
          value: 2,
          label: '类型2'
        }],

        tableRrait: [],
        dialogUpdateTrait: false,
        traitConfig: {
          id: '',
          config: {
            pros: {
              p1: '',
              p2: '',
              p3: '',
              p4: '',
              p5: '',
              p6: '',
              p7: '',
              p8: '',
              p9: '',
              p10: '',
              p11: '',
              p12: '',
              p13: '',
              p14: '',
              p15: ''
            }
          }
        },
        robotTotalWinLoseNdot: 0,
        total1: 0,
        total2: 0
      }
    },
    methods: {
      handleTools () {
        if (this.tools) {
          this.tools = false
        } else {
          this.tools = true
        }
      },
      handleFind () {
        let data = Qs.stringify(this.condition)

        this.$axios({
          method: 'post',
          url: '/api/AnalysisService.getRobotInfo',
          data
        })
        .then(resp => {
          if (resp.data.status === 'success') {

            this.tableData = resp.data.data
            this.condition.total = resp.data.total
          } else {
            that.$message({
              showClose: true,
              message: resp.data.cause,
              type: 'warning'
            })
          }
        })
      },
      handleCurrentChange (val) {
        console.log(val)
        this.condition.currentPage = val
        this.handleFind()
      },
      init () {
        this.handleFind()
      },
      showUpdateDymcProDialog (id, pro) {
        this.updateDymcPro.id = id
        this.updateDymcPro.pro = pro
        this.dialogUpdateDymcPro = true
      },
      handleUpdateDymcPro () {
        if (this.updateDymcPro.id === '' || this.updateDymcPro.pro === '') {
          return false
        }
        if (isNaN(this.updateDymcPro.pro) || !Number.isInteger(Number(this.updateDymcPro.pro))) {
          this.$message.error('请输入整数')
          return false
        }
        let data = Qs.stringify(this.updateDymcPro)

        this.$axios({
          method: 'post',
          url: '/api/GmService.updateDymcPro',
          data
        })
        .then(resp => {
          if (resp.data.status === 'success') {
            this.$message.success('修改成功')
            this.handleFind()
            this.dialogUpdateDymcPro = false
          } else {
            this.$message.error(resp.data.cause)
          }
        })
        .catch(e => {
          this.$message.error('服务器出小差了')
        })
      },
      updateGlobalDymcPro () {
        if (isNaN(this.globalDymcPro) || !Number.isInteger(Math.abs(Number(this.globalDymcPro)))) {
          this.$message.error('概率值错误')
          return false
        }
        let data = Qs.stringify({
          globalDymcPro: this.globalDymcPro,
          type: this.condition.type
        })
        this.$axios({
          method: 'post',
          url: '/api/GmService.updateGlobalDymcPro',
          data
        })
        .then(response => {
          if (response.data.status === 'success') {
            this.$message.success('修改成功')
            this.globalDymcPro = ''
            this.handleFind()
          } else {
            this.$message.error(response.data.cause)
          }
        })
        .catch(e => {
          console.log(e)
          this.$message.error('服务器出小差了')
        })
      },
      getRobotTrait () {
        this.$axios({
          method: 'get',
          url: '/api/GmService.getRobotTrait'
        })
        .then(response => {
          if (response.data.status === 'success') {
            this.tableRrait = response.data.data
          } else {
            this.$message.error(response.data.cause)
          }
        })
        .catch(e => {
          console.log(e)
          this.$message.error('服务器出小差了')
        })
      },
      showUpdateTraitDialog (config) {
        this.traitConfig = config
        this.dialogUpdateTrait = true
      },
      handleUpdateTrait () {
        console.log(this.traitConfig)
        let data = Qs.stringify({
          id: this.traitConfig.id,
          p1: this.traitConfig.config.pros.p1,
          p2: this.traitConfig.config.pros.p2,
          p3: this.traitConfig.config.pros.p3,
          p4: this.traitConfig.config.pros.p4,
          p5: this.traitConfig.config.pros.p5,
          p6: this.traitConfig.config.pros.p6,
          p7: this.traitConfig.config.pros.p7,
          p8: this.traitConfig.config.pros.p8,
          p9: this.traitConfig.config.pros.p9,
          p10: this.traitConfig.config.pros.p10,
          p11: this.traitConfig.config.pros.p11,
          p12: this.traitConfig.config.pros.p12,
          p13: this.traitConfig.config.pros.p13,
          p14: this.traitConfig.config.pros.p14,
          p15: this.traitConfig.config.pros.p15
        })
        this.$axios({
          method: 'post',
          url: '/api/GmService.updateRobotTrait',
          data
        })
        .then(response => {
          if (response.data.status === 'success') {
            this.$message.success('修改成功')
            this.dialogUpdateTrait = false
            this.getRobotTrait()
          } else {
            this.$message.error(response.data.cause)
          }
        })
        .catch(e => {
          console.log(e)
          this.$message.error('服务器出小差了')
        })
      },
      getRobotTotalWinLoseNdot () {
        this.$axios({
          method: 'get',
          url: '/api/GmService.getRobotTotalWinLoseNdot'
        })
        .then(response => {
          if (response.data.status === 'success') {
            this.robotTotalWinLoseNdot = response.data.data
          } else {
            this.$message.error(response.data.cause)
          }
        })
        .catch(e => {
          console.log(e)
          this.$message.error('获取机器人总输赢失败~服务器出小差了')
        })
      },
      getTotalUsedRobot () {
        this.$axios({
          method: 'get',
          url: '/api/GmService.getTotalUsedRobot'
        })
        .then(response => {
          if (response.data.status === 'success') {
            this.total1 = response.data.total1
            this.total2 = response.data.total2
          } else {
            this.$message.error(response.data.cause)
          }
        })
        .catch(e => {
          console.log(e)
          this.$message.error('获取机器人使用总数失败~服务器出小差了')
        })
      }
    },
    mounted () {
      this.getRobotTrait()
      this.getRobotTotalWinLoseNdot()
      this.getTotalUsedRobot()
      this.init()
    }
  }
</script>
